/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.comboBox;

import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.border.LineBorder;

import org.jxSwing.Cleanable;
import org.jxSwing.component.JXComponent;
import org.jxSwing.i18n.I18N;
import org.jxUtils.check.CheckUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.comboBox
 * @date 05/02/2014 - 22:27:00
 * @param <T>
 */
public class ComboBox<T> extends JComboBox<T> implements JXComponent, Cleanable {
	
	private static final long serialVersionUID = -5409072788980874013L;
	
	private boolean atributte = true;
	
	private boolean required = false;
	
	private boolean requiredBorderEnabled = true;
	
	private int index = -1;
	
	/**
	 *
	 */
	public ComboBox() {
	
		super();
		
		setSize(new Dimension(200, 25));
		setPreferredSize(new Dimension(200, 25));
	}
	
	/**
	 * @param items
	 */
	public ComboBox(final Collection<T> items) {
	
		super(new Vector<T>(items));
		
		setSize(new Dimension(200, 25));
		setPreferredSize(new Dimension(200, 25));
	}
	
	/**
	 * @param aModel
	 */
	public ComboBox(final ComboBoxModel<T> aModel) {
	
		super(aModel);
		
		setSize(new Dimension(200, 25));
		setPreferredSize(new Dimension(200, 25));
	}
	
	/**
	 * @param items
	 */
	public ComboBox(final T[] items) {
	
		super(items);
		
		setSize(new Dimension(200, 25));
		setPreferredSize(new Dimension(200, 25));
	}
	
	/**
	 * @param item
	 */
	@Override
	public void addItem(final T item) {
	
		super.addItem(item);
	}
	
	/**
	 * @param items
	 */
	public void addItems(final Collection<T> items) {
	
		//
		for(final T item : items) {
			
			addItem(item);
		}
	}
	
	/**
	 * @param items
	 */
	public void addItems(final T[] items) {
	
		//
		for(final T item : items) {
			
			addItem(item);
		}
	}
	
	@Override
	public void clear() {
	
		//
		if(getItemCount() > 0) {
			
			setSelectedIndex(0);
		}
	}
	
	/**
	 * @param value
	 * @return if value exists in items
	 */
	public boolean containsItem(final Object value) {
	
		//
		for(final T item : getItems()) {
			
			//
			if(item != null && item.equals(value)) {
				
				return true;
			}
		}
		
		return false;
	}
	
	@Override
	public int getIndex() {
	
		return index;
	}
	
	/**
	 * @return items
	 */
	public List<T> getItems() {
	
		final List<T> items = new ArrayList<>();
		
		//
		for(int index = 0; index < getItemCount(); index++) {
			
			items.add(getModel().getElementAt(index));
		}
		
		return items;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JComboBox#getSelectedItem()
	 */
	@Override
	@SuppressWarnings("unchecked")
	public T getSelectedItem() {
	
		return (T)super.getSelectedItem();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JComboBox#getSelectedObjects()
	 */
	@Override
	@SuppressWarnings("unchecked")
	public T[] getSelectedObjects() {
	
		return (T[])super.getSelectedObjects();
	}
	
	@Override
	public boolean isAtributte() {
	
		return atributte;
	}
	
	@Override
	public boolean isRequired() {
	
		return required;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isRequiredBorderEnabled()
	 */
	@Override
	public boolean isRequiredBorderEnabled() {
	
		return requiredBorderEnabled;
	}
	
	@Override
	public void setAtributte(final boolean atributte) {
	
		this.atributte = atributte;
	}
	
	@Override
	public void setIndex(final int index) {
	
		this.index = index;
	}
	
	/**
	 * @param items
	 */
	public void setItems(final Collection<T> items) {
	
		removeAllItems();
		
		addItem(null);
		addItems(items);
	}
	
	/**
	 * @param items
	 */
	public void setItems(final T[] items) {
	
		removeAllItems();
		
		addItem(null);
		addItems(items);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#setName(java.lang.String)
	 */
	@Override
	public void setName(final String name) {
	
		super.setName(name);
		
		setRequired(isRequired());
	}
	
	@Override
	public void setRequired(final boolean required) {
	
		this.required = required;
		
		//
		if(required) {
			
			//
			if(isRequiredBorderEnabled()) {
				
				setBorder(new LineBorder(Color.RED));
			}
			
			//
			if(CheckUtil.isNullOrAbsoluteEmpty(getName())) {
				
				setToolTipText(I18N.component().requiredThis());
				
			}else {
				
				setToolTipText(I18N.component().requiredField(getName()));
			}
			
		}else {
			
			setBorder(BorderFactory.createEmptyBorder());
			setToolTipText(null);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setRequiredBorderEnabled(boolean)
	 */
	@Override
	public void setRequiredBorderEnabled(final boolean requiredBorderEnabled) {
	
		this.requiredBorderEnabled = requiredBorderEnabled;
	}
}
